<?php
/**
 * Created by PhpStorm.
 * User: zcq
 * Date: 2018/10/15
 * Time: 9:19
 */

namespace app\admin\controller;


use app\admin\service\VoteService;
use app\common\RedisCache;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use think\Request;

class Vote extends Base
{
    public $voteService;
    public function __construct(Request $request = null,VoteService $voteService)
    {
        parent::__construct($request);
        $this->voteService = $voteService;
    }

    /**
     * @return mixed
     * 活动列表
     */
    public function index()
    {
        $keyword = input('keyword');
        $data = $this->voteService->getPaginate($keyword);
        $this->assign('data',$data);
        $this->assign('keyword',$keyword);
        return $this->fetch();
    }

    /**
     * @return mixed|string
     * 新增活动
     */
    public function add()
    {
        if(\request()->isPost()){
            $params = input();
            $res = $this->voteService->save($params);
            return dataReturn($res['code'],$res['msg']);
        }

        $this->assign('url',url('add'));
        $this->assign('data',1);
        $this->assign('title','新增');
        return $this->fetch('info');
    }

    /**
     * @return mixed|string
     * 编辑
     */
    public function update()
    {
        $params = input();
        $data = $this->check($params['id']??0);

        if(\request()->isPost()){
            $res = $this->voteService->save($params);
            return dataReturn($res['code'],$res['msg']);
        }
        $data->head_img = $data->head_img?:'';
        $data->link_url = \request()->domain()."/index/vote/index/vote_id/".$data->id . "/site/". session("admin")['site_id'];
        $data->vote_btn_name = $data->vote_btn_name?:'投票';
        $this->assign('url',url('update'));
        $this->assign('data',json_encode($data));
        $this->assign('title','编辑');
        return $this->fetch('info');
    }


    /**
     * 设置白名单
     */
    public function white()
    {
        $id = input('id');
        $vote = $this->check($id);
        $white = $this->voteService->getWhite($id);

        $this->assign('white',$white);
        $this->assign('vote',$vote);
        return $this->fetch();
    }

    public function ajaxAddWhite()
    {
        $id = input('id');
        $vote = $this->check($id);
        $params = input();
        $res = $this->voteService->addWhite($params);
        return dataReturn($res['code'],$res['msg']);

    }

    public function ajaxWhiteUpdate()
    {
        $id = input('id');
        $vote = $this->check($id);
        $params = input();
        $res = $this->voteService->WhiteUpdate($params);
        return dataReturn($res['code'],$res['msg']);

    }

    public function ajaxUpdateWhite()
    {
        $field = input('field');
        $value = input('value/d');
        $id = input('id/d');
        if($field == 'mobile' && !preg_match(config('mobile_reg'),$value)){
            return dataReturn(0,'电话号码格式不正确');
        }
        $res = $this->voteService->updateWhite($id,$field,$value);
        return dataReturn($res['code'],$res['msg']);

    }

    public function ajaxDelWhite()
    {
        $res = $this->voteService->delWhite(input('id/d'));
        return dataReturn($res['code'],$res['msg']);

    }


    private function check($id)
    {
        $vote = $this->voteService->check($id??0);
        if(!$vote)
            $this->error('不存在');
        return $vote;
    }


}